// This file contains material supporting section 3.7 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com 
package Client_Server;
import java.awt.EventQueue;
import java.io.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import Gui.CreateList;
import Gui.GuestWindow;
import Gui.LoginForm;
import Gui.MarketingDepartmentEmployee;
import Gui.MarketingDirectorGui;
import Gui.Salesman;
import Gui.mainf;

import client.*;
import common.*;
import java.io.*;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;


import Gui.GuestWindow;
import Gui.LoginForm;
import Gui.MarketingDepartmentEmployee;
import Gui.MarketingDirectorGui;
import Gui.MarketingTemplates;
import Gui.Salesman;
import Gui.UpdateCatalog;

import client.*;
import common.*;

// TODO: Auto-generated Javadoc
/**
 * This class constructs the UI for a chat client.  It implements the
 * chat interface in order to activate the display() method.
 */

public  class ClientConsole implements ChatIF 
{
	
	/** The a frame. */
	public GuestWindow aFrame;
	
	/** The form. */
	public LoginForm form;
	
	/** The tframe. */
	public MarketingDirectorGui tframe;
	
	/** The result. */
	private ArrayList<Object> result;
	
	/** The temp. */
	public MarketingTemplates temp;

  //Class variables *************************************************
  
  /**
   * The default port to connect on.
   */
  final public static int DEFAULT_PORT = 5555;
 
  //Instance variables **********************************************
  
  /**
   * The instance of the client that created this ConsoleChat.
   */
   static Client client;

  
  //Constructors ****************************************************

  /**
   * Constructs an instance of the ClientConsole UI.
   *
   * @param host The host to connect to.
   * @param port The port to connect on.
   */
  public ClientConsole(String host, int port) 
  {
    try 
    {
      client= new Client(host, port, this);
    } 
    catch(IOException exception) 
    {
      System.out.println("Error: Can't setup connection!"
                + " Terminating client.");
      System.exit(1);
    }
  }

  
  //Instance methods ************************************************
  
  /**
   * This method waits for input from the console.  Once it is 
   * received, it sends it to the client's message handler.
   */
  public void accept() 
  {
    try
    {


      GuestWindow aFrame = new GuestWindow(); // create MainWindow
	
      aFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );   
      aFrame.setSize( 1500,765 ); // set frame size
      aFrame.setVisible( true ); // display frame
    }
 
    catch (Exception ex) 
    {
      System.out.println
        ("Unexpected error while reading from console!");
    }
  }

  /**
   * This method overrides the method in the ChatIF interface.  It
   * displays a message onto the screen.
   *
   * @param msg message The string to be displayed.
   */
  public void display(String msg) 
  {
	  new mainf(msg);	
  }
  
  /* (non-Javadoc)
   * @see common.ChatIF#display(java.util.ArrayList)
   */
  public void display(ArrayList<Object> message) {
		setResult(message);
		 new mainf(message);
	}

	/**
	 * Sets the result.
	 *
	 * @param message the new result
	 */
	private void setResult(ArrayList<Object> message) {
			this.result = message;
	}

	/**
	 * Gets the result.
	 *
	 * @return the result
	 */
	public ArrayList<Object> getResult() {
	
		return this.result;
	}
  
  //Class methods ***************************************************
  
  /**
   * This method is responsible for the creation of the Client UI.
   *
   * @param args the arguments
   * @throws UnknownHostException the unknown host exception
   */
  public static void main(String[] args) throws UnknownHostException 
  {
    String host = "";
  

    try
    {
      host = args[0];
    }
    catch(ArrayIndexOutOfBoundsException e)
    {
      host = "localhost";
    }
    ClientConsole Console= new ClientConsole(host, DEFAULT_PORT);
    
    Console.accept();  //Wait for console data
  }
  /**
   * This method get an array from the GUI and sent it to handleMessageFromClientUI .
   *
   * @param arr The host to connect to.
   */

public static void HandleMessageFromGui(String arr[]) { 
	// TODO Auto-generated method stub
	client.handleMessageFromClientUI(arr);
}




}
//End of ConsoleChat class
